package com.winter.dao;

import com.winter.model.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.regex.Pattern;

/**
 * <B>中文类名：</B>活动数据层<BR>
 * <B>概要说明：</B><BR>
 *
 * @author: LH
 * @date: 14:36 2019/6/13
 */
@Component
public class ActivityDao {

    @Autowired
    private MongoTemplate mongoTemplate;


//    public List<Activity> find(ActivityQuery activityQuery) {
//        Sort sort = new Sort(Sort.Direction.ASC, "type")
//                .and(new Sort(Sort.Direction.DESC, "create_time"));
//        Query query = new Query();
//        query.with(sort);
//        query.addCriteria(
//                Criteria.where("status").ne(BaseStatusConstant.DELETED)
//                        .and("shop_id").is(activityQuery.getShopId()));
//        //动态拼接查询条件
//        if (!ObjectUtils.isEmpty(activityQuery.getName())) {
//            query.addCriteria(Criteria.where("name").is(activityQuery.getName()));
//        }
//        if(!ObjectUtils.isEmpty(activityQuery.getType())) {
//            query.addCriteria(Criteria.where("type").is(activityQuery.getType()));
//        }
//        return mongoTemplate.find(query, Activity.class);
//    }
//
//    public Activity findById(String id, String type, String shopId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("status").ne(BaseStatusConstant.DELETED));
//        if(!ObjectUtils.isEmpty(id)){
//            query.addCriteria(Criteria.where("_id").is(id));
//        }
//        if(!ObjectUtils.isEmpty(type)){
//            query.addCriteria(Criteria.where("type").is(Integer.parseInt(type)));
//            query.addCriteria(Criteria.where("shop_id").is(shopId));
//        }
//        return mongoTemplate.findOne(query, Activity.class);
//    }
//
    public void save(Activity activity) {
         mongoTemplate.save(activity);
    }
//
//    public APage<Activity> findAllDetailed(APage<Activity> page) {
//
////        Sort sort = Sort.by(Sort.Direction.DESC, "create_time");
//        Sort sort = new Sort(Sort.Direction.ASC, "type")
//                .and(new Sort(Sort.Direction.DESC, "create_time"));
//        Pageable pageable = PageRequest.of(page.getPageNo() - 1, page.getPageSize(), sort);
//        Query query = new Query();
//        query.addCriteria(
//                Criteria.where("status").ne(BaseStatusConstant.DELETED)
//                        .and("shop_id").is(page.getParam("shopId")));
//        //动态拼接查询条件
//        if (!ObjectUtils.isEmpty(page.getParam("name"))) {
//            Pattern pattern = Pattern.compile("^.*" + page.getParam("name").toString().trim() + ".*$", Pattern.CASE_INSENSITIVE);
//            query.addCriteria(Criteria.where("name").regex(pattern));
//        }
//        //计算总数
//        long total = mongoTemplate.count(query, Activity.class);
//        List<Activity> activities = mongoTemplate.find(query.with(pageable), Activity.class);
//        page.setList(activities);
//        page.setTotalCount((int) total);
//        return page;
//    }
//
//    public UpdateResult delete(String id, String sysUserId) {
//        Query query = new Query(Criteria.where("id").is(id));
//        Update update = new Update()
//                .set("status", BaseStatusConstant.DELETED)
//                .set("updated_user", sysUserId)
//                .currentDate("updated_time");
//        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Activity.class);
//        return updateResult;
//    }
//
//    public UpdateResult update(Activity activity) {
//        Query query = new Query(
//                Criteria.where("id").is(activity.getId())
////                .and("shop_id").is(activity.getShopId())
//        );
//        Update update = new Update();
//        update.currentDate("updated_time");
//        if(!ObjectUtils.isEmpty(activity.getName())) {
//            update.set("name", activity.getName());
//        }
//        if(!ObjectUtils.isEmpty(activity.getStatus())) {
//            update.set("status", activity.getStatus());
//        }
//        if(!ObjectUtils.isEmpty(activity.getStartDate())) {
//            update.set("start_date", activity.getStartDate());
//        }
//        if(!ObjectUtils.isEmpty(activity.getEndDate())) {
//            update.set("end_date", activity.getEndDate());
//        }
//        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Activity.class);
//        return updateResult;
//    }
//
//
//    public Activity findByName(String name, String shopId) {
//        Query query = new Query(
//                Criteria.where("name").is(name)
//                .and("shop_id").is(shopId)
//                .and("status").is(BaseStatusConstant.NORMAL)
//        );
//        return mongoTemplate.findOne(query, Activity.class);
//    }

    public List<Activity> findShopActivity(String shopId) {
        Query query = new Query(
                Criteria.where("shop_id").is(shopId)
        );
        return mongoTemplate.find(query, Activity.class);
    }

    public List<Activity> findActivityListByName(String name, String shopId) {
        Pattern pattern = Pattern.compile("^.*" + name.trim() + ".*$", Pattern.CASE_INSENSITIVE);
        Query query = new Query(
                Criteria.where("name").regex(pattern)
                        .and("shop_id").is(shopId)
        );
        return mongoTemplate.find(query, Activity.class);
    }

    public Activity findActivityById(Activity activity) {
        if(ObjectUtils.isEmpty(activity.getId())){
            return null;
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("shop_id").is(activity.getShopId()));
        query.addCriteria(Criteria.where("_id").is(activity.getId()));
        return mongoTemplate.findOne(query,Activity.class);
    }
}
